import java.io.*;
import java.util.*;

public class CustomerList extends ArrayList<Customer> {

    private AscendingNameComparator ascendingNameComparator;
    private DescendingNameComparator descendingNameComparator; 
    private DescendingIDComparator descendingIDComparator;

    public CustomerList() {
	super();
	ascendingNameComparator = new AscendingNameComparator();
	descendingNameComparator = new DescendingNameComparator();
	descendingIDComparator = new DescendingIDComparator();
    }

    public String toString() {
	String s = "";

	s += ("\n\n");
	s += ("                                     CUSTOMERS");
	s += ("\n----------------------------------------------------------------------------------------\n");
	s += String.format("%-12s %-20s %-26s %-27s","MemberID","Name","Street Address","City");
	s += ("\n----------------------------------------------------------------------------------------\n");
	for(int i = 0; i < size(); i++) {
	    s += String.format("%-12s %-20s %-26s %-27s",get(i).memberID,get(i).getName(),get(i).getAddress(),get(i).getCity());
	    s += "\n";
	}
	return s;
	// System.out.println("\n                                     CUSTOMERS");
	// System.out.println("\n----------------------------------------------------------------------------------------");
	// System.out.print("  ");
	// System.out.printf("%-9s %-18s %-26s %-27s","MemberID","Name","Street Address","City");
	// System.out.println("\n----------------------------------------------------------------------------------------");
	// for(int i = 0; i < size(); i++) {
	//     System.out.printf("%-9s %-18s %-26s %-27s",get(i).memberID,get(i).getName(),get(i).address(),get(i)getCity());
	// }
    }

    public void sortByIDA() {
	Collections.sort(this);
    }

    public void sortByIDD() {
	Collections.sort(this, descendingIDComparator);
    }

    public void sortByNameA() {
	Collections.sort(this, ascendingNameComparator);
    }
    
    public void sortByNameD() {
	Collections.sort(this, descendingNameComparator);
    }

    public class DescendingIDComparator implements Serializable, Comparator<Customer>{
	public int compare(Customer a, Customer b) {
	    return (b.memberID - a.memberID);
	}
    }

    public class AscendingNameComparator implements Serializable, Comparator<Customer>{
	public int compare(Customer a, Customer b) {
	    return (a.getName().compareTo(b.getName()));
	}
    }

    public class DescendingNameComparator implements Serializable, Comparator<Customer>{
	public int compare(Customer a, Customer b) {
	    return (-1*(a.getName().compareTo(b.getName())));
	}
    }

}